//定义电影演员相关接口
const express = require("express");
const router = express.Router();
//引入mysql连接池
const pool = require("../utils/db");
//引入joi表单验证模块
const Joi = require("joi");
//引入响应对象模块
const Response = require("../utils/Response");
//根据电影id查询演员
router.get("/movie-actors/movieid", (req, resp) => {
  let { movie_id } = req.query;
  let schema = Joi.object({
    movie_id: Joi.string().required(),
  });
  let { error, value } = schema.validate(req.query);
  if (error) {
    resp.send(Response.error(400, error));
    return;
  }
  //执行sql语句
  let sql = `select
        ma.id actor_id,
        mima.movie_id movie_id,
        ma.actor_name actor_name,
        ma.actor_avatar actor_avatar
   from movie_actor ma join movie_info_map_actor mima on ma.id = mima.actor_id
   where
        mima.movie_id = ?`;
  pool.query(sql, [movie_id], (error, result) => {
    if (error) {
      resp.send(Response.error(500, error));
      throw error;
    }
    resp.send(Response.ok(result));
  });
});

//模糊查询
//接口地址/movie-actor/name
router.post("/movie-actor/name", (req, resp) => {
  let { name } = req.body;
  //表单验证
  let schema = Joi.object({
    name: Joi.string().required(),
  });
  let { error, value } = schema.validate(req.body);
  if (error) {
    resp.send(Response.error(400, error));
    return;
  }
  let sql = "select * from movie_actor where actor_name like ?";

  pool.query(sql, [`%${name}%`], (error, result) => {
    if (error) {
      resp.send(Response.error(500, error));
      throw err;
    }
    resp.send(Response.ok(result));
  });
});

//删除演员接口
//接口地址/movie-actor/del
router.post("/movie-actor/del", (req, resp) => {
  let { id } = req.body;
  //表单验证
  let schema = Joi.object({
    id: Joi.string().required(),
  });
  let { error, value } = schema.validate(req.body);
  if (error) {
    resp.send(Response.error(400, error));
    return;
  }
  //执行删除业务
  let sql = "delete from movie_actor where id = ?";
  pool.query(sql, [id], (error, result) => {
    if (error) {
      resp.send(Response.error(500, error));
      throw error;
    }
    resp.send(Response.ok());
  });
});
//添加演员接口
//接口地址/movie-actor/add
router.post("/movie-actor/add", (req, resp) => {
  let { actorName, actorAvatar } = req.body;
  //表单验证
  let schema = Joi.object({
    actorName: Joi.string().required(),
    actorAvatar: Joi.string().required(),
  });
  let { error, value } = schema.validate(req.body);
  if (error) {
    resp.send(Response.error(400, error));
    return;
  }
  //表单验证通过，执行添加操作
  let sql = "insert into movie_actor (actor_name,actor_avatar) values (?,?)";
  pool.query(sql, [actorName, actorAvatar], (error, result) => {
    if (error) {
      resp.send(Response.error(500, error));
      throw err;
    }
    resp.send(Response.ok());
  });
});
// 获取所有演员列表;
router.get("/movie-actors", (req, resp) => {
  //获取请求参数 get请求的参数分装在req.query中
  let { page, pagesize } = req.query;
  //需要对服务端传入的值进行验证（服务端表单验证）(joi模块)
  //查询数据库
  let schema = Joi.object({
    page: Joi.number().required(), //page必须是数字，必填
    pagesize: Joi.number().integer().required(), //pagesize必须是不大于100的数字，必填
  });
  let { error, value } = schema.validate(req.query);
  if (error) {
    resp.send(Response.error(400, error));
    return;
  }
  let startIndex = (page - 1) * 10;
  let size = parseInt(pagesize);
  let sql = "select * from movie_actor limit ?,?";
  pool.query(sql, [startIndex, size], (error, result) => {
    if (error) {
      resp.send(Response.error(500, error));
      throw err;
    }
    resp.send(Response.ok(result));
  });
});

//将Router接口导出
module.exports = router;
